//商品列表
var app = getApp(); //全局APP

Page({
	//页面的初始数据
	data: {
		plan_year: 0,
		plan_season: 0,
		plan_class_id: 0,
		syrs: 0,
		checkboxAll: false,
		listData: [],
		selectNum: 0, 
		buttonDisabled: false // 按钮是否可用
	},

	//加载执行
	onLoad: function(options) {
		var plan_year = options.plan_year;
		var plan_season = options.plan_season;
		var plan_class_id = options.plan_class_id;
		var syrs = options.syrs;
		
		this.setData({
			plan_year: plan_year,
			plan_season: plan_season,
			plan_class_id: plan_class_id,
			syrs: syrs
		});
	},
	
	//加载执行
	onShow: function() {
		this.getDrawConfirmDetail();
	},
	
	getDrawConfirmDetail: function() {
		var page = this;
		var data = {
			plan_year: page.data.plan_year,
			plan_season: page.data.plan_season,
			plan_class_id: page.data.plan_class_id
		};
		
		// 获取班级
		app.api.getDrawConfirmDetail(data, function(res) {
			if (res.status) {
				page.setData({
					listData: res.data
				}); 
			} else {
				//报错了
				app.common.errorToBack(res.msg, 0);
				page.setData({
					listData: []
				});
			}
			page.isAllCheckbox();
		});
	},
	
	// 选择事件处理
	itemSelected: function(e) {
		var page = this;
		var xh = e.currentTarget.dataset.xh;
	
		var listData = page.data.listData;
		for (var i = 0; i < listData.length; i++) {
			if (listData[i].xh == xh) {
				if (listData[i].status == true) {
					listData[i].status = false;
				} else {
					listData[i].status = true;
				}
			}
		}
	
		page.setData({
			listData: listData
		});
	 
		page.isAllCheckbox();
	},
	
	//是否全选
	isAllCheckbox: function() {
		var page = this;
		var listData = page.data.listData;
		var flag = true;
		var num = 0;
	
		if (!listData && typeof(listData) != "undefined") {
			flag = false;
			num = 0;
		} else {
			for (var i = 0; i < listData.length; i++) {
				if (listData[i].status == false) {
					flag = false;
				} else {
					num += 1;
				}
			}
	
			if (listData.length <= 0) {
				flag = false;
				num = 0;
			}
		}
	
		this.setData({
			checkboxAll: flag,
			selectNum: num
		});
	},
	
	//全选操作
	checkboxAll: function(e) {
		if (this.data.checkboxAll == true) {
			this.setData({
				checkboxAll: false
			});
			this.setAllCheckbox(false);
		} else {
			this.setData({
				checkboxAll: true
			});
			this.setAllCheckbox(true);
		}
	},
	
	//全选设置
	setAllCheckbox: function(e) {
		var page = this;
		var listData = page.data.listData;
		if (e) {
			//全选
			for (var i = 0; i < listData.length; i++) {
				listData[i].status = true;
			}
		} else {
			//全不选
			for (var i = 0; i < listData.length; i++) {
				listData[i].status = false;
			}
		}
		page.setData({
			listData: listData
		});
	
		page.isAllCheckbox();
	},
	
	submit: function(e) {
		var page = this; 
		console.log(page.data.selectNum);
		if(page.data.selectNum == 0) {
			app.common.errorToBack("没有需要提交的学生！", 0);
			return;
		}
		
		if(page.data.selectNum > page.data.syrs) {
			app.common.errorToBack("班级剩余教材数量不足, 剩余数量为"+page.data.syrs+"！", 0);
			return;
		}
		
		app.common.buttonDisabled(this);
		
		var listData = page.data.listData;
		
		var xhData = '';
		var idData = '';
		var plan_dep_id = '';
		var plan_specialty_id = '';
		var plan_class_id = '';
		var plan_dir = '';
		for (var i = 0; i < listData.length; i++) {
			if (listData[i].status == true) {
				xhData += ',' + listData[i].xh;
				if(listData[i].applyDetailId != '') {
					idData += ',' + listData[i].applyDetailId;
				} 
				plan_dep_id = listData[i].plan_dep_id;
				plan_specialty_id = listData[i].plan_specialty_id;
				plan_class_id = listData[i].plan_class_id;
				plan_dir = listData[i].plan_dir;
			} 
		}
		
		if (xhData.substr(0, 1) == ',') {
			xhData = xhData.substr(1);
		}
		
		if (idData.substr(0, 1) == ',') {
			idData = idData.substr(1);
		}
	 
		var data = {
			id: idData, 
			xh: xhData,
			plan_year: page.data.plan_year,
			plan_season: page.data.plan_season,
			plan_dep_id: plan_dep_id,
			plan_specialty_id: plan_specialty_id,
			plan_class_id: plan_class_id,
			plan_dir: plan_dir
		};
		
		// 提交申请
		app.api.bookDetailDraw(data, function(res) {
			app.common.successToShow(res.msg, function() { 
				page.getDrawConfirmDetail();
			});  	 
			// 重置剩余教材数量
			// var leftNum = page.data.syrs - page.data.selectNum
			// page.setData({
			// 	syrs: leftNum
			// });
		});
	},
});
